/**
 * Filename: AlbumMemberAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/8 12:11
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.UserStatusActivity;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.bean.YNMember;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.service.RemoveMemberService;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import java.util.ArrayList;
import java.util.List;


public class AlbumMemberAdapter extends RecyclerView.Adapter<AlbumMemberAdapter.ViewHolder> {

    private Context mContext;
    private List<YNMember> datas;
    private ImageLoader imageLoader;
    private YNAlbum mGroup;
    private boolean remove = false;
    public List<String> uid = new ArrayList<>();
    public List<String> nickName=new ArrayList<>();
    private String curID;

    public AlbumMemberAdapter(Context mContext, List<YNMember> datas, YNAlbum mGroup, boolean remove, String curID) {
        this.mContext = mContext;
        this.datas = datas;
        this.mGroup = mGroup;
        imageLoader = ImageLoader.getInstance();
        this.remove = remove;
        this.curID = curID;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_selected;
        LinearLayout ll_item_album_member;                                //列表View
        ImageView iv_item_album_member_avatar;                        //成员头像
        TextView tv_item_album_member_username;                        //成员名

        public ViewHolder(View itemView) {
            super(itemView);
            ll_item_album_member = (LinearLayout) itemView
                    .findViewById(R.id.ll_item_album_member);
            iv_item_album_member_avatar = (ImageView) itemView
                    .findViewById(R.id.iv_item_album_member_avatar);
            tv_item_album_member_username = (TextView) itemView
                    .findViewById(R.id.tv_item_album_member_username);
            iv_selected = ((ImageView) itemView.findViewById(R.id.iv_selected));
        }

        public void bindData(final YNMember listItems) {
            if (listItems != null) {
                imageLoader.displayImage(listItems.getUser().getUpic() + StringUtils.getQulity(),
                        new ImageViewAware(iv_item_album_member_avatar, false),
                        ImageOptHelper.getAvatarOptions());
                tv_item_album_member_username.setText(listItems.getUser().getUnickname());
                if (remove) {
                    //消除复用
                    if (uid.contains(listItems.getUser().getUserid()+"")) {
                        iv_selected.setVisibility(View.VISIBLE);
                        iv_selected.setImageResource(R.drawable.remove_selected);
                    } else {
                        iv_selected.setVisibility(View.VISIBLE);
                        iv_selected.setImageResource(R.drawable.remove_no_selected);
                    }
                    ll_item_album_member.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (uid.contains(listItems.getUser().getUserid()+"")) {
                                iv_selected.setVisibility(View.VISIBLE);
                                iv_selected.setImageResource(R.drawable.remove_no_selected);
                                if (uid.size() != 0 && uid.contains(listItems.getUser().getUserid()+"")) {
                                    uid.remove(listItems.getUser().getUserid()+"");
                                    nickName.remove(listItems.getUser().getUnickname());
                                }
                            } else {
                                iv_selected.setVisibility(View.VISIBLE);
                                if (!curID.equals(listItems.getUser().getUserid()+"")){
                                    iv_selected.setImageResource(R.drawable.remove_selected);
                                    if (!uid.contains(listItems.getUser().getUserid()+"")) {
                                        uid.add(listItems.getUser().getUserid()+"");
                                        nickName.add(listItems.getUser().getUnickname());
                                    }
                                }else{
                                    iv_selected.setImageResource(R.drawable.remove_no_selected);
                                }
                            }
                            mContext.sendBroadcast(new Intent(ACTIONs.IsSelected));
                        }
                    });
                    //取消长按时间的监听
                    ll_item_album_member.setLongClickable(false);
                } else {
                    iv_selected.setVisibility(View.GONE);
                    // 点击列表
                    ll_item_album_member.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, UserStatusActivity.class);
                            intent.putExtra(CommonConstants.YNUSERID, listItems.getUser().getUserid());
                            intent.putExtra(CommonConstants.YNGROUPID, mGroup.getGroupid());
                            mContext.startActivity(intent);
                            Intent statintent = new Intent(ACTIONs.record);
                            statintent.putExtra("key", "成员头像_4");
                            statintent.putExtra("value", 1);
                            mContext.sendBroadcast(statintent);
                        }
                    });
                    ll_item_album_member.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            if (YNSingleton.getInstance().getCurAlbum().getGcreator().equals(curID)&&!curID.equals(listItems.getUser().getUserid()+"")) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                                builder.setMessage("确定要移除该成员吗？");
                                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent = new Intent(mContext, RemoveMemberService.class);
                                        intent.putExtra("userid", listItems.getUser().getUserid()+"");
                                        intent.putExtra("groupid", mGroup.getGroupid()+"");
                                        intent.putExtra("owner",YNSingleton.getInstance().getCurAlbum().getGcreator());
                                        intent.putExtra("nickName",listItems.getUser().getUnickname());
                                        mContext.startService(intent);
                                        dialog.dismiss();
                                    }
                                });
                                builder.create().show();
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                                builder.setMessage("对不起，您不能移除该成员！");
                                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                                builder.create().show();
                            }
                            return false;
                        }
                    });
                }
            }
        }
    }

    private YNMember getItem(int position) {
        return datas.get(position);
    }

    public void updateData(boolean remove){
        this.remove=remove;
    }
    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_album_member, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        YNMember listItems = getItem(position);
        holder.bindData(listItems);
        holder.itemView.setTag(listItems);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }
}
